<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary">添加角色</el-button>
      <el-table :data="rolesList" border style="width: 100%" stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              v-for="(item,i1) in scope.row.children"
              :key="item.id"
              :class="['bdBot',i1==0?'bdTop':'']"
            >
              <el-col :span="5">
                <el-tag closable @close="removeRightById(scope.row,item.id)">{{item.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row
                  v-for="(item2,i2) in item.children"
                  :key="item2.id"
                  :class="[i2==0?'':'bdTop']"
                >
                  <el-col :span="8">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRightById(scope.row,item2.id)"
                    >{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="16">
                    <el-tag
                      closable
                      type="warning"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      @close="removeRightById(scope.row,item3.id)"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete">删除</el-button>
            <el-button type="warning" icon="el-icon-setting" @click="editRights(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="分配权限" :visible.sync="showDialogRights" width="50%" @close="rightsDialogClose">
      <el-tree
        ref="treeRef"
        default-expand-all
        :data="rightsList"
        show-checkbox
        node-key="id"
        :props="defaultProps"
        :default-checked-keys="checkedIds"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showDialogRights = false">取 消</el-button>
        <el-button type="primary" @click="saveRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  mounted() {
    this.getRolesList()
  },
  data() {
    return {
      role: {},
      rolesList: [],
      showDialogRights: false,
      rightsList: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      checkedIds: []
    }
  },
  methods: {
    removeRightById(role, id) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${id}`)
          if (res.meta.status !== 200) {
            this.$message.error(res.meta.msg)
            return
          }
          this.$message.success(res.meta.msg)
          role.children = res.data
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async getRolesList() {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
        return
      }
      this.$message.success(res.meta.msg)
      this.rolesList = res.data
    },
    async editRights(role) {
      this.role = role
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.rightsList = res.data
      for (let i = 0; i < role.children.length; i++) {
        for (let j = 0; j < role.children[i].children.length; j++) {
          for (let q = 0; q < role.children[i].children[j].children.length; q++) {
            this.checkedIds.push(role.children[i].children[j].children[q].id)
          }
        }
      }
      console.log(this.checkedIds)

      this.showDialogRights = true
    },
    rightsDialogClose() {
      this.checkedIds = []
    },
    async saveRights() {
      let arr = [...this.$refs.treeRef.getHalfCheckedKeys(), ...this.$refs.treeRef.getCheckedKeys()]
      let rids = arr.join(',')
      const { data: res } = await this.$http.post(`roles/${this.role.id}/rights`, { rids })
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.showDialogRights = false
      this.getRolesList()
    }
  }
}
</script>
<style lang='less' scoped>
.bdTop {
  border-top: 1px solid #999;
}
.bdBot {
  border-bottom: 1px solid #999;
}
.el-row {
  .el-tag {
    margin: 10px;
  }
  display: flex;
  align-items: center;
}
</style>
